﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;

using VTV.TaskManagementSystem.Common;

namespace VTV.TaskManagementSystem.WebApp.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] != null && Session["UserName"] != null && Session["RoleLevel"] != null)
            {
                Response.Redirect("../Admin/Default.aspx");
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            var username = this.txtUsername.Text;
            var password = this.txtPassword.Text;

            var business = new BusinessLogic.User();
            Entities.Login login;
            if (business.CheckLogin(username, password, out login) && String.IsNullOrEmpty(business.ErrorMessage) && login != null)
            {
                Session["UserID"] = login.UserID;
                Session["UserName"] = login.UserName;
                Session["RoleLevel"] = login.RoleLevel;
                if (login.EmployeeID.HasValue)
                {
                    Session["EmployeeID"] = login.EmployeeID;
                }
                if (!String.IsNullOrEmpty(login.EmployeeName))
                {
                    Session["EmployeeName"] = login.EmployeeName;
                }
                if (!String.IsNullOrEmpty(login.RoleName))
                {
                    Session["RoleName"] = login.RoleName;
                }

                var linkReturn = Request.QueryString["return"];
                if(!String.IsNullOrEmpty(linkReturn))
                {
                    Response.Redirect(linkReturn);
                }

                Response.Redirect("../Admin/Default.aspx");
            }
            else
            {
                X.Msg.Notify("Thông báo", "Đăng nhập không thành công" + business.ErrorMessage).Show();
                this.txtPassword.Text = null;
                this.txtUsername.Text = null;
                this.txtUsername.Focus(true);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}